﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using Supersense.db;
using System.Data;

public partial class product : System.Web.UI.Page
{
    public DataTable productCategory;
    public DataTable products;
    public DataTable subCategory;
    public String currentType;
    public DataTable[] hArray;

    protected void Page_Load(object sender, EventArgs e)
    {
        int type = 0;
        string catid = Request.QueryString["catid"];
        Crud modelCategory = new Crud("ss_category", "id");
        productCategory = modelCategory.fList("type='product' and upid=0", "", "", "");
        DataTable[] htArray = new DataTable[productCategory.Rows.Count];
        hArray = htArray;
        if (Request.QueryString["type"] != null)
        {
            type = Convert.ToInt32(Request.QueryString["type"].ToString());
        }
        DataTable subCategory = modelCategory.fList("type='product' and upid!=0", "", "", "");
        for (int i = 0; i < productCategory.Rows.Count; i++)
        {
            htArray[i] = this.dealTable(subCategory, "upid", productCategory.Rows[i]["id"].ToString().Trim());
            //dt.Rows[i]["subCategory"] = this.dealTable(subCategory, "upid", dt.Rows[i].ToString());
        }

        
        currentType = type.ToString();
        subCategory = modelCategory.fList("type='product' and upid=" + type, "", "", "");
        Crud modelProduct = new Crud("ss_product", "id");
        string where = "";
        if (type != 0 || catid != null)
        {
            if (type != null)
            {
                where += " type=" + type;
            }
            if(catid != null)
            {
                where = where == "" ? " catid=" + catid : where + " and catid=" + catid;
            }
        }
        else
        {
            where = "mine = 1";
        }
        products = modelProduct.fList(where, "", "", "");
    }

    public DataTable dealTable(DataTable sourceTable, String key, String value)
    {
        DataTable ht = new DataTable();
        ht.Columns.Add("name");
        ht.Columns.Add("id");
        if(sourceTable == null){
            return null;
        }
        foreach (DataRow dr in sourceTable.Rows)
        {
            if (dr[key].ToString().Trim() == value)
            {
                DataRow row = ht.NewRow();
                row["name"] = dr["name"].ToString();
                row["id"] = dr["id"].ToString();
                ht.Rows.Add(row);
            }
        }
        return ht;
    }

}